import {IGameScene} from "./base/IGameScene";
import {PlayerData} from "./PlayerData";
import {Player} from "./base/Player";
import {TableData} from "./TableData";
import {ChatMessageType, PlayerStatus} from "../Constants";
import {HeroData} from "../data/HeroData";
import {network} from "../common/network";
import {cmd} from "../common/cmdClient";

export class PlayerPost{
    static view:IGameScene;
    static init(view:IGameScene){
        this.view = view;
        network.addHandler(cmd.onPlayerEnter,PlayerPost.onPlayerEnter,PlayerPost)
        network.addHandler(cmd.onPlayerLeave,PlayerPost.onPlayerLeave,PlayerPost)
        network.addHandler(cmd.onPlayerStatus,PlayerPost.onPlayerStatus,PlayerPost)
        network.addHandler(cmd.onTableInfo,PlayerPost.onTableInfo,PlayerPost)
        network.addHandler(cmd.onTableStatus,PlayerPost.onTableStatus,PlayerPost)
    }

    static exit(){

    }

    //处理播放协议
    static onReceiveMessage(event:string,msg:any) {
        if(event == "table_info") PlayerPost.onTableInfo(msg);
    }

    private static onPlayerEnter(playerInfo){
        let player = new Player();
        player.nickname=playerInfo.nickname;
        player.uid=playerInfo.uid;
        player.gold=playerInfo.gold;
        player.seat=playerInfo.seat;
        player.status=playerInfo.status;
        PlayerData.addPlayer(player);
        let localSeat = PlayerData.serverSeatToLocalSeat(player.seat);
        console.log(localSeat,player);
        PlayerPost.view.onPlayerEnter(localSeat,player);
    }

    private static onPlayerStatus(playerStatus){
        let player = PlayerData.getPlayerByUserID(playerStatus.uid);
        player.seat = playerStatus.seat;
        player.status = playerStatus.status;
        let localSeat = PlayerData.serverSeatToLocalSeat(player.seat);
        console.log(localSeat,player);
        PlayerPost.view.onPlayerStatus(localSeat,player)
    }

    private static onPlayerGameData(playerGameData){
        let player = PlayerData.getPlayerByUserID(playerGameData.userid);
        /*player.bodygold = playerGameData.bodygold;
        player.diamond = playerGameData.diamond;*/
        let localSeat = PlayerData.serverSeatToLocalSeat(player.seat);
        console.log(localSeat,player);
        PlayerPost.view.onPlayerGameData(localSeat,player)
    }

    private static onPlayerLeave(msg){
        let player = PlayerData.getPlayerByUserID(msg.uid);
        if(player){
            let localSeat = PlayerData.serverSeatToLocalSeat(player.seat);
            PlayerData.delPlayerByUserID(msg.uid);
            PlayerPost.view.onPlayerLeave(localSeat,player);
        }
    }

    private static onLeaveRoom(msg){
        PlayerPost.view.onLeaveRoom();
    }

    private static onChat(msg){
        if(msg.type ==ChatMessageType.VOICE && msg.userid == HeroData.uid) return;
        let player = PlayerData.getPlayerByUserID(msg.userid);
        if(player){
            let localSeat = PlayerData.serverSeatToLocalSeat(player.seat);
            PlayerPost.view.onChat(localSeat,msg.type,msg.content);
        }
    }

    private static onTableInfo(tableInfo){
        TableData.tableid = tableInfo.tableid;
        TableData.gamestatus = tableInfo.gamestatus;
        TableData.seatcount = tableInfo.seatcount;
        PlayerData.init(TableData.seatcount);
        PlayerPost.view.onGameInfo();
    }

    private static onTableStatus(tableStatus){
        TableData.gamestatus = tableStatus.gamestatus;
        PlayerPost.view.onGameStatusChanged();
    }
    ///////////////////////////////////////////////////////////////////////////////
    static enterTable(){
        network.sendMsg(cmd.game_main_enterTable)
    }

    static leaveRoom(){
        network.removeThisHandlers(this)
        // 用户离开
        network.sendMsg(cmd.game_main_leave);
        // 切场景
        cc.director.loadScene("LobbyScene");
    }

    static sendReady(){
        network.sendMsg(cmd.game_main_playStatus,{status:PlayerStatus.READY});
    }

    static sendStandUp(){

    }

    static sendChat(type,content){
        //GameSocketUtils.send("chat",{type:type,content:content});
    }
}